//
//  TZYMusicCell.m
//  Moment
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 片刻. All rights reserved.
//

#import "TZYMusicCell.h"
#define AUTOWIDTH  [[UIScreen mainScreen] bounds].size.width / 375
#define AUTOHEIGHT  [[UIScreen mainScreen] bounds].size.height / 667


@interface TZYMusicCell ()

@property(nonatomic, strong) UILabel *type;/**< Music 类型*/
@property(nonatomic, strong) UILabel *titleLb;/**< Music 标题*/
@property(nonatomic, strong) UILabel *name;/**< Music 作者名字*/
@property(nonatomic, strong) UILabel *content;/**< Music 文字详情*/
@property(nonatomic, strong) UILabel *zanLabel;/**< Music  赞*/
@property(nonatomic, strong) UIButton *heartButton;/**< Music  心型*/
@property(nonatomic, strong) UIImageView *playImageView;


@end

@implementation TZYMusicCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return self;
}

#pragma mark - 子视图
- (void)createSubView
{
    
    self.type = [[UILabel alloc] init];
    //self.type.backgroundColor = [UIColor yellowColor];
    [self.contentView addSubview:self.type];
    
    [self.type mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(@10);
        make.width.mas_equalTo(@150);
        make.height.equalTo(self.contentView.mas_height).multipliedBy(0.1);
        
    }];
    
    
    
    
    self.titleLb = [[UILabel alloc] init];
    //self.titleLb.backgroundColor = [UIColor orangeColor];
    [self.contentView addSubview:self.titleLb];

    [self.titleLb mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@30);
        make.top.mas_equalTo(self.type.mas_bottom).offset(0);
        make.width.mas_equalTo(SCREEN_WIDTH - 60);
        //make.height.mas_equalTo(@20);
        make.height.equalTo(self.contentView.mas_height).multipliedBy(0.3);
        
    }];
    
    
    
    self.name = [[UILabel alloc] init];
    //self.name.backgroundColor = [UIColor greenColor];
    [self.contentView addSubview:self.name];
    [self.name mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLb.mas_left);
        make.top.mas_equalTo(self.titleLb.mas_bottom).offset(0);
        make.width.mas_equalTo(SCREEN_WIDTH - 60);
        //make.height.mas_equalTo(@20);
        make.height.equalTo(self.contentView.mas_height).multipliedBy(0.1);
        
    }];
    
    
    
    
    
    self.playImageView = [[UIImageView alloc] init];
    //self.playImageView.backgroundColor = [UIColor yellowColor];
    [self.contentView addSubview:self.playImageView];
    self.playImageView.image = [UIImage imageNamed:@"ic_ting_play.png"];
    [self.playImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        //make.center.mas_equalTo(self.soundImageView.mas_bottom);
        make.top.mas_equalTo(@30);
        make.right.mas_equalTo(@(-20));
        make.width.mas_equalTo(@(SCREEN_WIDTH * (2.0 / 14)));
        make.height.mas_equalTo(@(SCREEN_WIDTH * (2.0 / 14)));
        
        
    }];
    
    //裁掉边框
    self.playImageView.backgroundColor = [UIColor colorWithRed:0.74 green:0.74 blue:0.74 alpha:1];
    self.playImageView.layer.borderColor = [UIColor colorWithRed:0.74 green:0.74 blue:0.74 alpha:1].CGColor;
    self.playImageView.layer.masksToBounds = YES;
    //设置半径
    self.playImageView.layer.cornerRadius = SCREEN_WIDTH * (2.0 / 28);
    //边框宽度
    self.playImageView.layer.borderWidth = 0;

    
    
    
    
    
    self.zanLabel = [[UILabel alloc] init];
    //self.zanLabel.backgroundColor = [UIColor purpleColor];
    [self.contentView addSubview:self.zanLabel];
    [self.zanLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(@(-20));
        make.width.mas_equalTo(@60);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-10);
        make.height.equalTo(self.contentView.mas_height).multipliedBy(0.1);
    }];
    
    
    
    
    self.heartButton = [[UIButton alloc] init];
    //self.heartButton.backgroundColor = [UIColor cyanColor];
    [self.contentView addSubview:self.heartButton];
    
    [self.heartButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.zanLabel.mas_left).offset(-10);
        make.width.mas_equalTo(@20);
        make.height.mas_equalTo(@20);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-10);
        
    }];
    
    
     [self.heartButton addTarget:self action:@selector(likeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.content = [[UILabel alloc] init];
    //self.content.backgroundColor = [UIColor purpleColor];
    [self.contentView addSubview:self.content];
    
    [self.content mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.name.mas_left);
        make.top.mas_equalTo(self.name.mas_bottom).offset(0);
        make.width.mas_equalTo(SCREEN_WIDTH - 60);
        make.bottom.equalTo(self.zanLabel.mas_top).offset(-5);
        
    }];
    
    
}




#pragma mark- 喜欢按钮实现方法
- (void)likeBtnAction:(UIButton *)btn{
    
    NSMutableArray *array = [[CollectViewController collectDataBase]selectCollectList];
    
    NSMutableArray *array2 = [NSMutableArray array];
    
    for (TZYHomeData *model in array) {
        [array2 addObject:model._id];
    }
    if ([array2 containsObject: self.homeData._id]) {
        
        TZYHomeData *model = [[TZYHomeData alloc]init];
        model.title = self.homeData.title;
        [[CollectViewController collectDataBase] deleteMyCollectData:model.title];
        
        // 关键帧动画,改变view缩放属性
        CAKeyframeAnimation *keyAnimtion = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        // 设置关键帧的值,values是数组类型
        keyAnimtion.values = @[@(1.5), @(1.0), @(0.5)];
        keyAnimtion.duration = 0.2;
        [btn.layer addAnimation:keyAnimtion forKey:@"key"];
        
        [self.heartButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xinxihuan00-2.png"] forState:UIControlStateNormal];
        self.zanLabel.text = [NSString stringWithFormat:@"%ld",[(self.zanLabel.text)integerValue] - 1];
        
    }else{
        TZYHomeData *model = [[TZYHomeData alloc]init];
        model._id = self.homeData._id;
        model.title = self.homeData.title;
        model.coverimg = self.homeData.coverimg;
        model.name = self.homeData.name;
        
        [[CollectViewController collectDataBase]inserCollectList:model];
        
        
        // 关键帧动画,改变view缩放属性
        CAKeyframeAnimation *keyAnimtion = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        // 设置关键帧的值,values是数组类型
        keyAnimtion.values = @[@(0.5), @(1.0), @(1.5)];
        keyAnimtion.duration = 0.2;
        [btn.layer addAnimation:keyAnimtion forKey:@"key"];
        // 2.点击按钮时做判断
        [self.heartButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xinxihuan002.png"] forState:UIControlStateNormal];
        self.zanLabel.text = [NSString stringWithFormat:@"%ld",[(self.zanLabel.text)integerValue] + 1];
    }
}










- (void)setHomeData:(TZYHomeData *)homeData
{
    if (_homeData != homeData) {
        _homeData = homeData;
    }
    
    
    //音乐 music
    self.type.text = [NSString stringWithFormat:@"%@ · %@",self.homeData.name,self.homeData.enname];
    self.type.font = [UIFont systemFontOfSize:[TZYFontSize getFontSizeForSmall]];
    
    
    //2
    self.titleLb.text = self.homeData.title;
    self.titleLb.font = [UIFont boldSystemFontOfSize:[TZYFontSize getFontSizeForBig]];
    self.titleLb.numberOfLines = 0;
    
    //3
    self.name.text = [NSString stringWithFormat:@"by:%@ 主播",[self.homeData.userinfo objectForKey:@"uname"]];
    self.name.font = [UIFont systemFontOfSize:[TZYFontSize getFontSizeForSmall]];
    
    
  
    
    
    
    //4
    self.content.text = self.homeData.content;
    self.content.font = [UIFont systemFontOfSize:[TZYFontSize getFontSizeForSmall]];
    self.content.numberOfLines = 0;
    
    
    //5
    self.zanLabel.text = [NSString stringWithFormat:@"%@",self.homeData.like];
    self.zanLabel.font = [UIFont systemFontOfSize:[TZYFontSize getFontSizeForSmall]];
    
   
    
    // 上拉下拉cell会重用cell,所以需要在赋值的时候加判断
    [self.heartButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xinxihuan00-2.png"] forState:UIControlStateNormal];
    if (_homeData.selected) {
        [self.heartButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xinxihuan002.png"] forState:UIControlStateNormal];
    }
    
    
    
    //判断收藏状态
    NSMutableArray *array = [[CollectViewController collectDataBase]selectCollectList];
    NSMutableArray *array2 = [NSMutableArray array];
    for (TZYHomeData *model in array) {
        [array2 addObject:model._id];
    }
    if ([array2 containsObject: self.homeData._id]) {
        [self.heartButton  setImage:[UIImage imageNamed:@"iconfont-xinxihuan002.png"] forState:UIControlStateNormal];
    }
    


}




- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
